﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HighSign.Common.Plugins;

namespace HighSign.CorePlugins.HotKey
{
	public partial class HotKeyUI : Form
	{
		#region Private Variables

		HotKeySettings _Settings = null;
		Keys _KeyCode;
		IHostControl _HostControl = null;

		#endregion

		#region Constructors

		public HotKeyUI()
		{
			InitializeComponent();

			this.TopLevel = false;
			this.FormBorderStyle = FormBorderStyle.None;
			this.AutoScroll = true;
			this.Dock = DockStyle.Fill;

			this.txtKey.KeyDown += new KeyEventHandler(txtKey_KeyDown);
			this.txtKey.GotFocus += new EventHandler(txtKey_GotFocus);
			this.txtKey.LostFocus += new EventHandler(txtKey_LostFocus);
		}

		public HotKeyUI(HotKeySettings KeySettings) : this()
		{
			Settings = KeySettings;
		}

		#endregion

		#region Events

		protected void txtKey_GotFocus(object sender, EventArgs e)
		{
			_HostControl.AllowEscapeKey = false;
		}

		protected void txtKey_LostFocus(object sender, EventArgs e)
		{
			_HostControl.AllowEscapeKey = true;
		}

		protected void txtKey_KeyDown(object sender, KeyEventArgs e)
		{
			e.Handled = true;
			e.SuppressKeyPress = true;

			if (e.Control || e.Shift || e.Alt || e.KeyCode == Keys.LWin || e.KeyCode == Keys.RWin)
				return;

			_KeyCode = e.KeyCode;
			txtKey.Text = new ManagedWinapi.KeyboardKey(_KeyCode).KeyName;
		}

		#endregion

		#region Public Properties

		public HotKeySettings Settings
		{
			get
			{
				_Settings = new HotKeySettings();
				_Settings.Windows = chkWin.Checked;
				_Settings.Control = chkControl.Checked;
				_Settings.Shift = chkShift.Checked;
				_Settings.Alt = chkAlt.Checked;
				_Settings.KeyCode = _KeyCode;

				return _Settings;
			}
			set
			{
				_Settings = value;

				if (_Settings == null)
					_Settings = new HotKeySettings();

				chkWin.Checked = _Settings.Windows;
				chkControl.Checked = _Settings.Control;
				chkShift.Checked = _Settings.Shift;
				chkAlt.Checked = _Settings.Alt;
				_KeyCode = _Settings.KeyCode;
				txtKey.Text = _Settings.KeyCode.ToString();
			}
		}

		public IHostControl HostControl
		{
			get { return _HostControl; }
			set { _HostControl = value; }
		}

		#endregion
	}
}
